package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.BusinessService;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UpdateById extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    String upid;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UpdateById frame = new UpdateById();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UpdateById() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 730, 536);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("\u8BF7\u8F93\u5165\u8981\u4FEE\u6539\u7684id");
        lblNewLabel.setBounds(49, 55, 218, 40);
        contentPane.add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(222, 63, 285, 24);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("\u8BF7\u8F93\u5165\u4FEE\u6539\u540E\u7684\u4FE1\u606F");
        lblNewLabel_1.setBounds(197, 119, 178, 18);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("password");
        lblNewLabel_2.setBounds(59, 173, 95, 18);
        contentPane.add(lblNewLabel_2);

        JLabel lblBusinessname = new JLabel("businessName");
        lblBusinessname.setBounds(59, 219, 133, 18);
        contentPane.add(lblBusinessname);

        JLabel lblBusinessaddress = new JLabel("businessAddress");
        lblBusinessaddress.setBounds(59, 266, 133, 18);
        contentPane.add(lblBusinessaddress);

        JLabel lblBusinessexplain = new JLabel("businessExplain");
        lblBusinessexplain.setBounds(59, 310, 120, 18);
        contentPane.add(lblBusinessexplain);

        JLabel lblStar = new JLabel("starPrice");
        lblStar.setBounds(59, 345, 120, 18);
        contentPane.add(lblStar);

        JLabel lblDeliverprice = new JLabel("deliverPrice");
        lblDeliverprice.setBounds(59, 382, 120, 18);
        contentPane.add(lblDeliverprice);

        textField_2 = new JTextField();
        textField_2.setBounds(272, 170, 177, 24);
        contentPane.add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(272, 216, 177, 24);
        contentPane.add(textField_3);

        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(272, 263, 177, 24);
        contentPane.add(textField_4);

        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(272, 307, 177, 24);
        contentPane.add(textField_5);

        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(272, 342, 177, 24);
        contentPane.add(textField_6);

        textField_7 = new JTextField();
        textField_7.setColumns(10);
        textField_7.setBounds(272, 379, 177, 24);
        contentPane.add(textField_7);

        JButton btnNewButton = new JButton("\u786E\u5B9A");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                upid=textField.getText();

                System.out.println("确定");
            }
        });
        btnNewButton.setBounds(557, 62, 113, 27);
        contentPane.add(btnNewButton);

        JButton button = new JButton("\u4FEE\u6539");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int id = Integer.parseInt(upid);
                BusinessService businessService=new BusinessServiceImpl();
                Object queryById = businessService.getQueryById(id);
                if(queryById!=null){
                    Business business= (Business) queryById;
                    business.setPassword(textField_2.getText());
                    business.setBusinessName(textField_3.getText());
                    business.setBusinessAddress(textField_4.getText());
                    business.setBusinessExplain(textField_5.getText());
                    String starPrice = textField_6.getText();
                    double star1 = Double.parseDouble(starPrice);
                    String DeliveryPrice = textField_7.getText();
                    double deliver1 = Double.parseDouble(DeliveryPrice);
                    business.setStarPrice(star1);
                    business.setDeliveryPrice(deliver1);
                    System.out.println("修改成功");

                    businessService.Update(business);
                }

                System.out.println("修改");
            }
        });
        button.setBounds(164, 438, 113, 27);
        contentPane.add(button);

        JButton button_1 = new JButton("\u53D6\u6D88");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("取消");
                MainWindow mainWindow = new MainWindow();
                mainWindow.setVisible(true);
                UpdateById.this.dispose();
            }
        });
        button_1.setBounds(462, 438, 113, 27);
        contentPane.add(button_1);
    }

}
